<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item :label="$t('orderNumber')" prop="orderSn">
            <el-input
              v-model="queryParams.orderSn"
              :placeholder="$t('pleaseEnterTheOrderNumber')"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item :label="$t('userName')" prop="userName">
            <el-input
              v-model="queryParams.userName"
              :placeholder="$t('enterOneUserName')"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item :label="$t('phoneNumber')" prop="userPhone">
            <el-input
              v-model="queryParams.userPhone"
              :placeholder="$t('pleaseEnterYourMobileNumber')"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item :label="$t('status')" prop="overStatus">
            <el-select
              v-model="queryParams.overStatus"
              :placeholder="$t('status')"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in overStatusList"
                :key="item.value"
                :label="checkStatusName(item.name)"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item :label="$t('collectionResults')" prop="remark">
            <el-select v-model="queryParams.remark" :placeholder="$t('pleaseSelect')" clearable>
              <el-option
                v-for="dict in dict.type.over_des"
                :key="dict.label"
                :label="checkName(dict.label)"
                :value="dict.label"
              />
            </el-select>
          </el-form-item>

          <el-form-item :label="$t('dueTime')">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              :start-placeholder="$t('startTime')"
              :end-placeholder="$t('endTime')"
            ></el-date-picker>
          </el-form-item>


          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
                $t("search")
              }}
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("reset") }}</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column :label="$t('platform')" align="center" prop="companyId"/>
          <el-table-column :label="$t('orderNumber')" align="center" prop="orderSn"/>
          <el-table-column :label="$t('userName')" align="center" prop="userName"/>
          <el-table-column :label="$t('phoneNumber')" align="center" prop="userPhone"/>
          <el-table-column :label="$t('status')" align="center" prop="status">
            <template slot-scope="scope">
              <span v-if="scope.row.status == '6'">{{ $t('orderStatus.repaymentInProgress') }}</span>
              <span v-if="scope.row.status == '8'">{{ $t('orderStatus.overdue') }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('loanAmount')" align="center" prop="realApplyMoney"/>
          <el-table-column :label="$t('amountPayable')" align="center" prop="realApplyMoney">
            <template slot-scope="scope">
              {{ scope.row.realApplyMoney + scope.row.overMoney - scope.row.alreadyRepayMoney }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('overdueDays')" align="center" prop="overDay"/>
          <el-table-column :label="$t('overdueAmount')" align="center" prop="overMoney"/>
          <el-table-column label="催收人" align="center" prop="collectionBy">
            <template slot-scope="scope">
              <span v-for="item in overUserList" v-if="scope.row.collectionBy == item.userId">{{ item.userName }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('virtualCardInformation')" align="center" prop="id">
            <template slot-scope="scope">
              <el-button type="text" @click="openUserVa(scope.row)">
                <el-button size="danger">{{ $t('virtualCard') }}</el-button>
              </el-button>
            </template>
          </el-table-column>
          <el-table-column :label="$t('timeOfApplication')" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('payTime')" align="center" prop="applyTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.applyTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('dueTime')" align="center" prop="repaymentTime" width="160">
            <template slot-scope="scope">
              <span>{{ scope.row.repaymentTime }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('collectionResults')" align="center" prop="remark">
            <template slot-scope="scope">
              <span>{{ checkName(scope.row.remark) }}</span>
            </template>
          </el-table-column>
          <el-table-column  :label="$t('details')" align="center" prop="detail"/>
          <el-table-column label="备注时间" align="center" prop="overRemarkTime" width="160">
            <template slot-scope="scope">
              <span>{{ scope.row.overRemarkTime }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('operate')"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-search"
              >
                <router-link :to="'/app/user-info/index/' + scope.row.id + '/' + scope.row.userId" class="link-type">
                  <span>{{ $t('see') }}</span>
                </router-link>
              </el-button>
              <!--<el-button-->
              <!--size="mini"-->
              <!--type="text"-->
              <!--icon="el-icon-edit"-->
              <!--@click="handlePay(scope.row)"-->
              <!--&gt;还款-->
              <!--          </el-button>-->
              <el-button
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="handleAdd(scope.row)"
              >添加备注
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <el-dialog title="催收记录" :visible.sync="open1" width="600px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="120px">
        <el-form-item label="备注" prop="remark">
          <el-select v-model="form1.remark" placeholder="请选择" clearable>
            <el-option
              v-for="dict in dict.type.over_des"
              :key="dict.label"
              :label="checkName(dict.label)"
              :value="dict.label"
            />
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('details')" prop="detail">
          <el-input v-model="form1.detail"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRemarkHandler">确 定</el-button>
        <el-button @click="open1 = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listMyOverApply, handlePayment, addOverLogForm} from "@/api/app/apply";
import {getUserVa} from "@/api/app/user"
import {listOverUser} from "@/api/system/user";

export default {
  name: "MyOver",
  dicts: ['over_des'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      applyList: null,
      // 公司数据
      companyList: null,
      overUserList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      form1: {},
      overStatusList: [
        {
          name: 'overStatus.toBeCollected',
          value: '0'
        },
        {
          name: 'overStatus.hasBeenCollected',
          value: '1'
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userPhone: undefined,
        userName: undefined,
        overStatus: undefined,
        orderSn: undefined,
        remark: undefined
      },
      // 表单校验
      rules: {
        paymentMoney: [
          {required: true, message: this.$t('paymentMoneyIsNotNull'), trigger: "blur"},
        ]
      },
      rules1: {
        remark: [
          {required: true, message: "备注不能为空", trigger: "blur"},
        ],
      }
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      if (this.$store.state.user.companyId) {
        this.queryParams.companyId = this.$store.state.user.companyId
      }
      listMyOverApply(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.applyList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    getOverUserList() {
      listOverUser().then(response => {
          this.overUserList = response.data;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        realApplyMoney: 0,
        manageMoney: 0,
        riskMoney: 0,
        applyServiceCharge: 0,
        applyCycle: 0,
        realPayMoney: 0,
        interestMoney: 0,
        overDay: 0,
        overMoney: 0,
        paymentMoney: 0
      };
      this.form1 = {
        id: undefined,
        remark: undefined,
        detail: undefined
      };
      this.resetForm("form");
      this.resetForm("form1");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    openUserVa(row) {
      //获取虚拟卡信息
      getUserVa({applyId: row.id}).then(response => {
          var str = '<h3>' + this.$t('userName') + '：' + response.data.accountName + '</h3>';
          if (!isNaN(response.data.bank)) {
            str += '<h3>' + this.$t('idNumber') + '：' + response.data.accountNo + '</h3>';
          } else {
            str += '<h3>' + this.$t('idNumber') + '：' + response.data.bank + '</h3>';
          }
          str += '<h3>' + this.$t('bank') + '：PVCOMBANK</h3>';
          this.$alert(str, this.$t('virtualCardInformation'), {
            dangerouslyUseHTMLString: true
          });
        }
      );

    },

    checkName(name) {
      if (this.$te('overResult.' + name)) {
        return this.$t('overResult.' + name)
      }
      return name;
    },
    checkStatusName(name) {
      if (this.$te(name)) {
        return this.$t(name)
      }
      return name;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.form1.id = row.id;
      this.open1 = true;
    },
    /** 修改按钮操作 */
    handlePay(row) {
      this.reset();
      // const id = row.id;
      // getApply(id).then(response => {
      this.form = row;
      this.form.realPayMoney = this.form.realApplyMoney - this.form.manageMoney - this.form.riskMoney - this.form.applyServiceCharge;
      this.open = true;
      this.title = "还款";
      // });
    },

    submitHandlerPayment() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let that = this.form;
          this.$modal.confirm('是否确认对编号为"' + this.form.orderSn + '"的订单还款？').then(function () {
            return handlePayment(that.id, '0', that.paymentMoney);
          }).then(() => {
            this.getList();
            this.open = false;
            this.$modal.msgSuccess("还款成功");
          }).catch((e) => {
          });
        }
      })
    },

    submitRemarkHandler() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          addOverLogForm(this.form1).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open1 = false;
            this.getList();
          });
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.id || this.ids;
      // this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
      //   return delIndexApply(ids);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("删除成功");
      // }).catch(() => {
      // });
    },

    // handleMoney(){
    //   this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
    // }
  }
};
</script>
